package com.feng.swing.tools.service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Properties;

import org.apache.commons.codec.binary.Base64;

import com.feng.security.dsa.SignatureHelper;
import com.feng.security.dsa.SignatureMan;
import com.feng.security.dsa.comon.DSAKeyMan;
import com.feng.security.dsa.comon.SignatureResult;
import com.feng.swing.tools.ui.TabPanel;

public class ToolsTabPanelService{
	public static final String AMS_CHARACTER_SET_UTF8 = "utf-8";

	public void urlDecode(TabPanel view) {
		try {
			String charset = (String) view.urlCharSet.getSelectedItem();
			System.out.println(">>>Get CharSet:" + charset);
			String data = new String(view.urlEncodeData.getText().getBytes(),charset);
			System.out.println(">>>Get data:" + data);
			String result = URLDecoder.decode(data,charset);
			System.out.println(">>>End result:" + result);
			view.urlEncodeResult.setText(result);
		}catch (UnsupportedEncodingException e) {}
	}

	public void urlEncode(TabPanel view) {
		try {
			String charset = (String) view.urlCharSet.getSelectedItem();
			System.out.println(">>>Get CharSet:" + charset);
			String data = view.urlEncodeData.getText();
			System.out.println(">>>Get data:" + data);
			String result = URLEncoder.encode(data,charset);
			System.out.println("<<<End result:" + result);
			view.urlEncodeResult.setText(result);
		}catch (UnsupportedEncodingException e) {}
	}
	
	public void base64DecodeBtnbase64Decode(TabPanel view) {
		String charset = (String) view.base64CharSet.getSelectedItem();
		System.out.println(">>>Get CharSet:" + charset);
		String data = view.base64Data.getText();
		System.out.println("<Get data:" + data);
		String result = new String(Base64.decodeBase64(data.getBytes()));
		System.out.println(">>>End result:" + result);
		view.base64Result.setText(result);
	}

	public void base64EncodeBtnbase64Encode(TabPanel view) {
		String charset = (String) view.base64CharSet.getSelectedItem();
		System.out.println(">>>Get CharSet:" + charset);
		String data = view.base64Data.getText();
		String result = new String(Base64.encodeBase64(data.getBytes(), true));
		System.out.println("<<<End result:" + result);
		view.base64Result.setText(result);
	}
	
	public void checkSign(TabPanel view) {
		String toCheckString = view.toCheckData.getText();
		String sign = view.toCheckSign.getText();
		String puKey = view.checkSignPublicKey.getText();
		StringBuffer msg = new StringBuffer();
		System.out.println("<toCheckString is :" + toCheckString);
		System.out.println("<toCheckSign is :" + sign);
		System.out.println("<publicKey is :" + puKey);
		try{
			SignatureHelper sh = new SignatureHelper();
			Properties checkSignPro = sh.getPropertiesFromQueryString(
					toCheckString, AMS_CHARACTER_SET_UTF8);
			SignatureMan sinatureMan = new SignatureMan();
			SignatureResult var = sinatureMan.checkDSASign(checkSignPro,puKey,"utf-8");
			msg.append(var.isSuccess());
		}catch(Exception e){
			msg.append("Check sign Error:"+e.toString());
		}
		view.checkSingResult.setText(msg.toString());
	}

	public void toSignBtnGetSign(TabPanel view) {
		String toCheckString = view.toSignData.getText();
		String priKey = view.toSignPrivateKey.getText();
		StringBuffer msg = new StringBuffer();
		System.out.println("<toCheckString is :" + toCheckString);
		System.out.println("<privateKey is :" + priKey);
		try{
			SignatureHelper sh = new SignatureHelper();
			Properties properties = sh.getPropertiesFromQueryString(toCheckString,
					AMS_CHARACTER_SET_UTF8);
			SignatureMan sinatureMan = new SignatureMan();
			DSAKeyMan keyMan = new DSAKeyMan();
			SignatureResult rt = keyMan.genDSAKeyPair();
			if (priKey != null && !priKey.equals("")) {
				rt = sinatureMan.dsaSign(properties, priKey, AMS_CHARACTER_SET_UTF8);
				msg.append("Sign:" + rt.getSign());
				msg.append("\nBase64Encode Sign:"+ URLEncoder.encode(rt.getSign(),AMS_CHARACTER_SET_UTF8));
			}
		}catch(Exception e){
			msg.append("Sign Error:"+e.toString());
		}
		view.toSignResult.setText(msg.toString());
	}
	public static void main(String [] args){
		SignatureHelper sh = new SignatureHelper();
		String toCheckString = "sign=MCwCFGFrzwcDytlBmaqWfjzLV28V6BNlAhQH/mB2dNZCoLTcfJwyIyHWspzQWQ==&pay_result=true&sign_type=DSA&prorderno=9110&input_charset=utf-8&notify_id=48774";
		Properties checkSignPro = sh.getPropertiesFromQueryString(
				toCheckString , AMS_CHARACTER_SET_UTF8);
		SignatureMan sinatureMan = new SignatureMan();
		String puKey = "MIIBtzCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYQAAoGAfDd/LhHPQlrEsB4pL0Wb2aJ0W0Rtj5v9YALluNCzBJtxUfTmrA6qqxCooDylcanNHUEUJrHkPlqrmaoCQlJlNNpj357qLVQmRpBTCWeJZS6HGQOzm7olL8sPUTWYpnD+LQg3xyxx9TooxtPQhiTbDlEHc6/mRnEWgdv87aNjueo=";
		SignatureResult var = sinatureMan.checkDSASign(checkSignPro,puKey ,"utf-8");
		System.out.println(var.isSuccess());
	}
}
